home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / language / isetl.arc / atom.t < prev    next >
Text File  |  1987-08-20  |  2KB  |  75 lines

  1. program tree_to_dag;
  2.     $represent a graph using mapping from atom to [op,left,right]
  3.  
  4.     print "Enter an expression with operators +-*/";
  5.     print "Parentheses may be used for grouping";
  6.     print "Arguments are single lower case letters";
  7.  
  8.     read expr;
  9.     print expr;
  10.  
  11.     multipliers := "*/";
  12.     adders      := "+-";
  13.     vars        := "abcdefghijklmnopqrstuvwxyz";
  14.  
  15.     operands := [];        $ operand stack
  16.     operators := [];        $ operator stack
  17.     tree    := {};        $ graph representing the expression
  18.  
  19.     for c in expr | c /= " " do
  20.     if c in vars   then
  21.         operands := [c] + operands;
  22.  
  23.     elseif c in adders then
  24.         while operators(1)?" " in multipliers do
  25.         left := newat;
  26.         right := newat;
  27.         [operators,      operands, tree(left), tree(right)]
  28.         := [operators(2..), 
  29.             [ [operators(1),left,right]] + operands(3..),
  30.             operands(2),
  31.             operands(1) ];
  32.         end;
  33.         operators := [c] + operators;
  34.  
  35.     elseif c in multipliers then
  36.         operators := [c] + operators;
  37.  
  38.     elseif c = "("   then
  39.         operators := [c] + operators;
  40.  
  41.     elseif c = ")"   then
  42.         while operators(1) /= "(" do
  43.         left := newat;
  44.         right := newat;
  45.         [operators,      operands, tree(left), tree(right)]
  46.         := [operators(2..), 
  47.             [ [operators(1),left,right]] + operands(3..),
  48.             operands(2),
  49.             operands(1) ];
  50.         end;
  51.         operators := operators(2..);
  52.  
  53.     else
  54.         print [c, "skipped"];
  55.     end;
  56.     end;
  57.  
  58.     while operators /= [] do
  59.     left := newat;
  60.     right := newat;
  61.     [operators,      operands, tree(left), tree(right)]
  62.     := [operators(2..), 
  63.         [ [operators(1),left,right]] + operands(3..),
  64.         operands(2),
  65.         operands(1) ];
  66.     end;
  67.  
  68.     root := newat;
  69.     tree(root) := operands(1);
  70.  
  71.     print [["root", root], ["tree", tree]];
  72. end;
  73.  
  74. "a + b * c + d * ( x + y ) * e";
  75.